<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Str;
class MembersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户管理')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('修改用户信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->model()->orderBy('updated_at', 'desc');
        $grid->id('id');
        $grid->name('昵称');
        $grid->email('邮箱');
        // $grid->email_verified_at('Email verified at');
        $grid->avatar('头像')->display(function($avatar){
            return Str::startsWith($avatar, 'http') ? $avatar : 'https://pic.touxiangge.com/' . $avatar;
        })->image(60, 60);
        $grid->introduction('个人简介');
        // $grid->password('Password');
        $grid->weapp_openid('小程序 openid');
        // $grid->weixin_session_key('Weixin session key');
        // $grid->remember_token('Remember token');
        $grid->created_at('注册时间');
        $grid->updated_at('最后登录');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->email('Email');
        $show->email_verified_at('Email verified at');
        $show->avatar('Avatar');
        $show->introduction('Introduction');
        $show->password('Password');
        $show->weapp_openid('Weapp openid');
        $show->weixin_session_key('Weixin session key');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', '昵称');
        $form->email('email', '邮箱');
        $form->text('avatar', '头像');
        $form->text('introduction', '个人简介');
        return $form;
    }
}
